package com.huanglj.himalaya.fragments;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.activitys.DetailActivity;
import com.huanglj.himalaya.activitys.MainActivity;
import com.huanglj.himalaya.adapters.AlbumAdapter;
import com.huanglj.himalaya.base.BaseFragment;
import com.huanglj.himalaya.interfaces.ISubscriptionCallback;
import com.huanglj.himalaya.presenters.AlbumDetailPresenter;
import com.huanglj.himalaya.presenters.PlayerPresenter;
import com.huanglj.himalaya.presenters.SubscriptionPresenter;
import com.huanglj.himalaya.views.ComfirmDialog;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.List;

public class SubscriptionFragment extends BaseFragment implements ISubscriptionCallback {

    private RecyclerView mSubRecyclerView;
    private AlbumAdapter mAdapter;
    private SubscriptionPresenter mSubscriptionPresenter;

    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        View view = layoutInflater.inflate(R.layout.fragment_subscription, container, false);
        TwinklingRefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(false);
        mSubRecyclerView = view.findViewById(R.id.recycler_subscription_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
        mSubRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AlbumAdapter();
        mSubRecyclerView.setAdapter(mAdapter);
        mSubscriptionPresenter = SubscriptionPresenter.getInstance();
        mSubscriptionPresenter.registerViewCallBack(this);
        mSubscriptionPresenter.getSubscriptionList();
        mAdapter.setOnRecommendItemClickListener(new AlbumAdapter.OnRecommendItemClickListener() {
            @Override
            public void onItemClick(Integer tag, Album album) {
                AlbumDetailPresenter.getInstance().setTargetAlbum(album);
                Intent intent = new Intent(getContext(), DetailActivity.class);
                startActivity(intent);
            }
        });
        mAdapter.setOnLongClickListerer(new AlbumAdapter.OnLongClickListerer() {
            @Override
            public void onLongClick(final Album album) {
                ComfirmDialog dialog = new ComfirmDialog(getActivity());
                dialog.show();
                dialog.setOnDialogClickListener(new ComfirmDialog.OnDialogClickListener() {
                    @Override
                    public void cancelClick() {
                        //删除订阅内容
                        mSubscriptionPresenter.deleteSubscription(album);
                    }

                    @Override
                    public void giveUpClick() {
                        //取消操作

                    }
                });

            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mSubscriptionPresenter.unRegisterViewCallBack(this);
        mAdapter.setOnRecommendItemClickListener(null);
    }

    @Override
    public void onAddResult(boolean success) {

    }

    @Override
    public void onDeleteResult(boolean success) {
        Toast toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT);
        toast.setText("取消订阅成功");
        toast.show();
    }

    @Override
    public void onGetListLoad(List<Album> result) {
        if (mAdapter != null) {
            mAdapter.setData(result);
        }
    }
}
